"""普通工具类"""
import time 
import json
from datetime import datetime, timedelta
import uuid
from uuid import uuid4



class Tools(object):
    
    @staticmethod
    def get_uuid4_no_line():
        """获取无横线的uuid4"""
        return str(uuid4()).replace("-", "")
    
    @staticmethod
    def str_to_uuid_str(string):
        """字符串转为uuid字符串"""
        try:
            uuid_obj = uuid.UUID(string)
            return str(uuid_obj)
        except ValueError:
            return None
    
    @staticmethod
    def get_timeStamp_scope(minute):
        """获取当前时间之前指定时间的时间戳"""
        current_time = datetime.now()
        start_time = int(time.mktime((current_time - timedelta(minutes=minute)).timetuple()))
        end_time = int(time.mktime(current_time.timetuple()))
        print(start_time, end_time)
        return start_time, end_time
    
    @staticmethod
    def timeStamp_to_strftime(timestamp):
        """时间戳转时间"""
        return time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(timestamp))
    
    @staticmethod
    def get_future_time_str(seconds):
        """获取某个时间点，返回字符串形式"""
        current_time = datetime.now()
        future_time = current_time + timedelta(seconds=seconds)
        return future_time.strftime("%Y-%m-%d %H:%M:%S")
    
    @staticmethod
    def get_current_time_str():
        """获取当前时间，返回字符串形式"""
        # 获取当前时间
        current_time = datetime.now()
        return current_time.strftime('%Y-%m-%d %H:%M:%S')
    
    @staticmethod
    def comparison_time(time_str1, time_str2):
        """对比两个时间"""
        time1 = datetime.strptime(time_str1, '%Y-%m-%d %H:%M:%S')
        time2 = datetime.strptime(time_str2, '%Y-%m-%d %H:%M:%S')
        # 比较两个 datetime 对象的大小
        return time1 < time2 or time1 <= time2
    
    @staticmethod
    def update_topology_json(topology_json, state_json=None, all_state=None):
        """更新json中的state状态"""
        if state_json is None:
            state_json = {}
        if  isinstance(topology_json, str):
            topology_json = json.loads(topology_json)
        uuid_key = ["vm_uuid", "subnet_id", "router_id"]
        node_list = topology_json["nodeList"]
        for node  in node_list:
            if all_state and node["type"] != 'switch' and node["type"] != 'router':
                node["state"] = all_state
                continue
            for uuid in uuid_key:
                if node.get(uuid):
                    value_uuid = node[uuid]
                    state = state_json.get(value_uuid, None)
                    if state:
                        node["state"] = state
                    break
        return topology_json
            
        

if __name__ == "__main__":
    # from basic_tools import Tools
    print(Tools.get_uuid4_no_line())